<!-- 
  @name: 客户
  @date: 2020.3.3
 -->
<template>
	<view class="content">
		<!-- 主账号权限 -->
		<view v-show="userMain === 1">
			<view class="fixed_top">
				<!-- 搜索区域 -->
				<view class="search">
					<view class="doview">
						<view class="i"></view>
						<input class="int" placeholder="输入客户名称或手机号" confirm-type="search" placeholder-class="phcolor" v-model.trim="keyword" />
						<view class="btn1" @tap="search">搜索</view>
					</view>
					<view class="btn2" @tap="$navigateTo('./mbadd')">添加</view>
				</view>
				
				<!-- 筛选区域 -->
				<view class="primary"></view><!-- 顶部背景色 -->
				<view class="select">
					<view class="item" :class="userType !== -1 ? 'sl' : ''" @tap="byType">{{typeText}}</view>
					<view class="_triangle atop" v-show="showType"></view>
					<view class="_triangle abtm" v-show="!showType"></view>
					
					<view class="line"></view>
					
					<view class="item" :class="showTol ? 'sl' : ''" @tap="byTol">加注金额</view>
					<view class="up_down_arr" :class="showTol ? 'std' : ''"></view>
				</view>
				
				<view class="selbtn" v-if="showType">
					<view
						class="btn"
						v-for="(item, index) in typeList"
						:key="index"
						:class="item.code === userType ? 'sl' : ''"
						@tap="chooseType(item, index)">
						{{item.text}}
					</view>
				</view>
			</view>
			
			<!-- 列表 -->
			<view class="list-card" :class="showType? 'h2' : ''">
				<view
					class="list"
					v-for="(item, index) in lists"
					:key="item.userId"
					@tap="$navigateTo('./cdetails?userId=' + item.userId)">
					<image class="img" :src="item.userPhoto"></image>
					<view class="info">
						<view class="msg">
							<view>{{item.userName}}</view>
							<view class="btm">{{item.userPhone}}</view>
						</view>
						<view class="icon i1" v-if="item.userType === 1">充值客户</view>
						<view class="icon i2" v-else-if="item.userType === 2">免单客户</view>
					</view>
				</view>
			</view>
			
			<!-- 底部加载提示 -->
			<btm-load></btm-load>
		</view>
		
		<!-- 非主账号权限 -->
		<view v-show="userMain !== 1">
			<view class="no-right">
				<view class="img"></view>
				<view class="tip">暂无权限</view>
			</view>
		</view>
		
		<!-- 显示于tabbar之上，渲染阴影效果 -->
		<view class="tabbar_shadow"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',  //搜索关键字
				userType: -1,  //客户类型
				showType: false,  //是否显示类型筛选区
				typeText: '客户类型',
				typeList: [
					{code: -1, text: '全部客户'}, {code: 1, text: '充值客户'},
					{code: 0, text: '普通用户'}, {code: 2, text: '免单客户'}
				],  //类型
				showTol: false,  //是否根据金额排列
				lists: [],  //列表
				page: 1,  //页码
				userMain: -1,  //账号权限，0-子账号，1-主账号，2-加注站职员
			}
		},
		onLoad() {
			this.userMain = this.$store.state.userMain;
			if(this.userMain === 1) {
				if(this.$store.state.userinfo) {
					this.getList();
				}
				uni.$on('client_addMember', this.addMember);
				uni.$on('client_changeType', this.changeType);
				uni.$on('client_deleteUser', this.deleteUser);
			}
		},
		onShow() {
			this.userMain = this.$store.state.userMain;
		},
		onUnload() {
			if(this.userMain === 1) {
				uni.$off('client_addMember', this.addMember);
				uni.$off('client_changeType', this.changeType);
				uni.$off('client_deleteUser', this.deleteUser);
			}
		},
		onReachBottom() {
			this.$store.commit('btmLoad', true);
			this.getList();
		},
		onPullDownRefresh() {
			if(this.userMain === 1) {
				this.addMember();
			} else {
				uni.stopPullDownRefresh();
			}
		},
		methods: {
			//监听是否添加了用户
			addMember() {
				this.keyword = '';
				this.userType = -1;
				this.showTol = false;
				this.search();
			},
			//搜索(筛选)
			search() {
				this.showType = false;
				this.lists = [];
				this.page = 1;
				this.getList();
				// if(this.keyword==''){
					
				// } else {
				// 	this.doSearch();
				// }
			},
			doSearch() {
				this.$http({
					url: this.$api.userUrl + 'getuserinfobyuserphone',
					data: {
						userPhone: this.phone,
					}
				}).then(res => {
					this.info = res;
				});
			},
			//监听改变用户类型
			changeType(e) {
				let index = this.lists.findIndex(item => item.userId === e.userId);
				this.lists[index].userType = parseInt(e.userType);
			},
			//监听是否删除客户
			deleteUser(e) {
				let index = this.lists.findIndex(item => item.userId == e.userId);
				this.lists.splice(index, 1);
			},
			//根据金额排列
			byTol() {
				this.showTol = !this.showTol;
				this.search();
			},
			//点击展开类型区
			byType() {
				this.showType = !this.showType
			},
			//选择类型
			chooseType(item, index) {
				this.userType = item.code;
				this.typeText = item.code === -1 ? '客户类型' : item.text;
				this.search();
			},
			//获取列表
			getList() {
				this.$http({
					url: this.$api.clientUrl + 'getfilloperatorlist',
					data: {
						userType: this.userType < 0 ? '' : this.userType,
						fillType: this.showTol ? 1 : 0,
						keyword: this.keyword,
						page: this.page,
						rows: this.$tmp.ROWS_RET
					},
					loading: this.page > 1 ? 0 : ''
				}).then(res => {
					let arr = res;
					if(arr && arr.length > 0) {
						this.lists = this.lists.concat(arr);
						this.page += 1;
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	/* 搜索区域 */
	.search {
		width: 750rpx;
		height: 100rpx;
		padding-left: 40rpx;
		background-color: $color-primary;
		display: flex;
		flex-direction: row;
		align-items: center;
		.doview {
			width: 560rpx;
			height: 60rpx;
			border-radius: 30rpx;
			background-color: $color-white;
			display: flex;
			flex-direction: row;
			align-items: center;
			.i {
				width: 29rpx;
				height: 29rpx;
				margin-left: 20rpx;
				background-image: url('http://img.etubang.com/group1/M00/00/41/rBCky15lnRqAG9eZAAAEgMBvtvE395.png');
			}
			.int {
				flex: 1;
				margin-left: 15rpx;
				font-size: 24rpx;
			}
			.btn1 {
				width: 80rpx;
				height: 60rpx;
				line-height: 54rpx;
				text-align: center;
				font-size: 24rpx;
				color: $color-grey-dark;
				margin-right: 10rpx;
			}
		}
		.btn2 {
			width: 80rpx;
			height: 60rpx;
			line-height: 54rpx;
			text-align: center;
			color: $color-white;
			margin-left: 40rpx;
		}
	}
	
	/* 筛选区域 */
	.primary {
		width: 750rpx;
		height: 26rpx;
		background-color: $color-primary;
	}
	.select {
		width: 750rpx;
		height: 100rpx;
		margin-top: -24rpx;
		border-radius: 24rpx 24rpx 0 0;
		background-color: $color-white;
		border-bottom: 1rpx solid $color-bg;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0 40rpx;
		.item.sl {
			color: $color-primary;
		}
		.line {
			width: 0;
			height: 36rpx;
			border-left: 1rpx solid $color-list;
			margin: 0 30rpx;
		}
	}
	.selbtn {
		width: 750rpx;
		height: 104rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		background-color: $color-bg;
		.btn {
			width: 140rpx;
			height: 50rpx;
			line-height: 50rpx;
			text-align: center;
			font-size: 24rpx;
			margin-left: 30rpx;
			border-radius:25px;
			background-color: $color-white;
		}
		.btn.sl {
			color: $color-white;
			background-color: $color-primary;
		}
	}
	
	/* 列表 */
	.list-card.h2 {
		margin-top: 304rpx;
	}
	.list-card {
		width: 750rpx;
		margin-top: 200rpx;
		.list {
			width: 750rpx;
			background-color: $color-white;
			display: flex;
			flex-direction: row;
			align-items: center;
			.img {
				width: 60rpx;
				height: 60rpx;
				margin-left: 50rpx;
				border-radius: $border-radius-circle;
			}
			.info {
				flex: 1;
				padding: 28rpx 0;
				margin-left: 40rpx;
				padding-right: 40rpx;
				border-bottom: 1rpx solid $color-list;
				display: flex;
				flex-direction: row;
				align-items: center;
				.msg {
					flex: 1;
					.btm {
						margin-top: 16rpx;
						font-size: 24rpx;
						color: $color-grey;
						font-family: $font-family-num;
					}
				}
				.icon {
					width: 110rpx;
					height: 40rpx;
					line-height: 40rpx;
					text-align: center;
					font-size: 20rpx;
					border-radius: 20rpx;
				}
				.icon.i1 {
					color: #FF9600;
					background-color: #FFF2CC;
				}
				.icon.i2 {
					color: $color-primary;
					background-color: #CCDCFF;
				}
			}
		}
		.list:last-child {
			.info {
				border-bottom: none;
			}
		}
	}
	
	/* 暂无权限 */
	.no-right {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: $color-white;
		.img {
			width: 260rpx;
			height: 276rpx;
			margin-top: 280rpx;
			background-image: url('http://img.etubang.com/group1/M00/00/42/rBCky16FttCASimjAAB5wUKuXik374.png');
		}
		.tip {
			margin-top: 18rpx;
			font-size: 30rpx;
			color: $color-primary;
		}
	}
</style>
